<?php 
namespace Common\Service\Sms;
/**
 * Xinxin短信接口
 * @author liuyang
 *
 */    
use Common\ServiceInterface\SmsService;
use Common\Util\HttpUtil;
class  SmsXinxinServiceImpl implements SmsService { 
    //private $app_id = '182999800000039655';// 应用ID
    const NAME = '1417681839'; 
    const PASS="BF7FAD6BF389D51317BF88943846";
    const API_URL="http://web.1xinxi.cn/asmx/smsservice.aspx";
    const SIGN="xx";
    private $timestamp=''; //时间戳，格式为：yyyy-MM-dd hh:mm:ss
    private $phone='';
    private $code; //验证码格式必须为6位的纯数字
    private $codelen = 6; //验证码长度必须为6位
    private $charset = "0123456789"; //验证码的随机因子
    private $exp_time=2; //验证码有效期长度，单位是分钟，可以为空，默认有效2分钟
    
    /** 
     * (non-PHPdoc)
     * @see SmsService::sendInfo()
     * return int
     */
    public function sendInfo($phone,$content){  
        $this->phone=$phone;
        $this->createCode();
        $rand=0; 
        if($this->checkPhone($this->phone)){
            $rstval=$this->send($content);    
            $rstval = explode(',', $rstval);
            if ($rstval[0]==0){ //从发送短信的方法中返回的obj中读取发送成功与否的标识
                $rand=$this->getCode(); 
            } 
        } 
        return $rand;
    }
     
    
    //检查参数
    private function checkPhone($phone){
        if (preg_match('/^(1[0-9]{10})$/',$phone)){
            $this->phone=$phone;
            return true;
        }else{
            return false;
        }
    }
    //生成随机码
    private function createCode() {
        $_len = strlen($this->charset)-1;
        for ($i=0;$i<$this->codelen;$i++) {
            $this->code .= $this->charset[mt_rand(0,$_len)];
        }
    }
     
    //获取验证码
    private function getCode() {
        return $this->code;
    }
    
    
    
    /**
     * send
     * @return Ambigous <boolean, mixed>
     */
    private function send($content) { 
         $content = str_replace("[code]", $this->code, $content);
         $param['name']    = self::NAME;
         $param['pwd']     = self::PASS;
         $param['content'] = $content;
         $param['mobile']  = $this->phone;
         $param['sign']    = self::SIGN;
         $param['type']    = "pt";  
         return HttpUtil::curlPost(self::API_URL,$param);
    }
  
    
}